import { OneModal } from '@/components/one-ui';
import { IOneModalRef } from '@/pages/type';
import { Ref, useContext, useEffect, useRef, useState } from 'react';
import { UpdateAssetService } from '../services/UpdateAssetService';
import { materialTableConfig } from '../mconfig/UpdateTableColumns';
import { message } from 'antd';
import { onCloseModalCommon } from '@/utils';
import { showTotalInfo } from '@/common/constVariable';
import { UpdateTableCommon } from './UpdateTableCommon';
import { AssetListManageAPI } from '../common/type';
import { AssetListManageContext } from './useManageContext';
import { IAssetListActionEnum } from './useManageReducer';

export const OpenAssetModal: React.FC<AssetListManageAPI.IOpenMaterialModal> = ({ curRes, onClose, isShowModal }) => {
  /** 弹框的ref */
  const updateModalRef: Ref<IOneModalRef> = useRef(null);
  const { selectAssetList, styleFixCls, assetListDispatch } = useContext(AssetListManageContext);

  /** 获取列表 */
  const { dataList, data, params, loading, pagination, searchList, loadMdicMapObj, getManageData } = UpdateAssetService(curRes);

  /** 暂时保存选中的值 */
  const [checkAssetList, setCheckAssetList] = useState<any>([]);

  /** 搜索 */
  const onSearch = (payload: Record<string, any>) => {
    getManageData({ ...payload });
  };

  /** 重置 */
  const onReset = (payload: Record<string, any>) => {
    getManageData({ ...payload });
  };

  /** 分页change 方法 */
  const onPageChange = (current: number, pageSize: number) => {
    getManageData({ ...params, current, pageSize });
  };

  /** 点击选中 */
  const onSelectTable = (selectInfo: Record<string, any>[]) => {
    setCheckAssetList(selectInfo);
  };

  /** 初始化 */
  useEffect(() => {
    getManageData({});
    if (selectAssetList.length === 0) {
      setCheckAssetList([]);
    } else {
      setCheckAssetList(selectAssetList);
    }
    // assetListDispatch({
    //   type: IAssetListActionEnum.SELECT_ASSET,
    //   payload: selectAssetList,
    // });
  }, [isShowModal]);

  /** 点击确定 */
  const onSave = () => {
    assetListDispatch({
      type: IAssetListActionEnum.SELECT_ASSET,
      payload: checkAssetList,
    });
    onCloseModalCommon(updateModalRef);
  };

  return (
    <>
      <OneModal
        open={true}
        getContainer={false}
        ref={updateModalRef}
        title={'资产列表'}
        onCancel={onClose}
        width={document.body.clientWidth * 0.65}
        bodyStyle={{
          height: document.body.clientHeight * 0.55,
          padding: 10,
        }}
        onOk={onSave}
      >
        <UpdateTableCommon
          styleFixCls={styleFixCls}
          searchProps={{
            searchList,
            onReset,
            onSearch,
          }}
          selectTablePorps={{
            onSelectTable,
            selectRowObjList: checkAssetList,
            selectType: 'checkbox',
            setCheckRows: setCheckAssetList,
          }}
          tableProps={{
            columns: materialTableConfig(),
            loading,
            dataSource: dataList ? dataList : [],
            pagination,
            onPageChange,
            rowKey: 'id',
            tableTotalShow: (total: any, range: any) => {
              return (
                <div
                  style={{
                    display: 'flex',
                    width: '100%',
                    justifyContent: 'space-between',
                  }}
                >
                  <div>
                    已选择<strong style={{ color: 'red' }}> {checkAssetList?.length} </strong>条资产
                  </div>
                  <div>{showTotalInfo(total, range)}</div>
                </div>
              );
            },
          }}
        />
      </OneModal>
    </>
  );
};
